//
//  GHSOrderTableViewCell.m
//  penzer
//
//  Created by 还带大道 on 2018/11/15.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GHSOrderTableViewCell.h"

@interface GHSOrderTableViewCell()
@property (weak, nonatomic) IBOutlet UIView *content_bgV;
@property (weak, nonatomic) IBOutlet UIView *image_bgV;
@property (weak, nonatomic) IBOutlet UIView *tagsView;
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UIButton *statusBtn;
@property (weak, nonatomic) IBOutlet UILabel *price1;
@property (weak, nonatomic) IBOutlet UILabel *price2;
@property (weak, nonatomic) IBOutlet UILabel *number;
@property (weak, nonatomic) IBOutlet UILabel *timer;
@property (weak, nonatomic) IBOutlet UIButton *handleBtn;
@property (weak, nonatomic) IBOutlet UILabel *desL;
@property (nonatomic,strong) NSMutableArray<UILabel *> *tagItems;

@end

@implementation GHSOrderTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    //设置阴影
    _content_bgV.backgroundColor = [UIColor whiteColor];
    _content_bgV.layer.shadowColor = RZ_COLOR_ALPHA(0, 0, 0, 0.1).CGColor;
    _content_bgV.layer.cornerRadius = 2.5;
    _content_bgV.layer.shadowOffset = CGSizeMake(0,1);
    _content_bgV.layer.shadowOpacity = 1;
    _content_bgV.layer.shadowRadius = 5;
    
    
    _image_bgV.backgroundColor = [UIColor whiteColor];
    _image_bgV.layer.shadowColor = RZ_COLOR_ALPHA(0, 0, 0, 0.07).CGColor;
    _image_bgV.layer.cornerRadius = 5;
    _image_bgV.layer.shadowOffset = CGSizeMake(0,1);
    _image_bgV.layer.shadowOpacity = 1;
    _image_bgV.layer.shadowRadius = 5;
    
    //添加渐变色
    [self setupStatusBtn];
    [self addTags];
    
    _title.text = @"新款iPad 银色 WiFi版 32G";
    self.tagsView.backgroundColor = [UIColor whiteColor];

}


- (void)setupStatusBtn
{
    _statusBtn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
    [_statusBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _statusBtn.backgroundColor = [UIColor clearColor];
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = self.statusBtn.layer.bounds;
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(1, 1);
    gl.colors = @[(__bridge id)RZ_COLOR(48, 35, 174).CGColor,(__bridge id)RZ_COLOR(200, 109, 215).CGColor];
    gl.locations = @[@(0),@(1.0f)];
    [self.statusBtn.layer insertSublayer:gl atIndex:0];
    
    //指定圆角位置
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.statusBtn.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomLeft cornerRadii:CGSizeMake(2.5, 2.5)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.statusBtn.bounds;
    maskLayer.path = maskPath.CGPath;
    gl.mask = maskLayer;
}



- (void)setType:(GHSOrderType)type
{
    _type = type;
    _desL.hidden = YES;
    _handleBtn.hidden = NO;
    _statusBtn.userInteractionEnabled = NO;
    
    switch (type) {
        case GHSOrderType_DFH:{
            NSString *timer = @"18:00";
            NSMutableAttributedString *mas = [NSMutableAttributedString new];
            NSAttributedString *as1 = [[NSAttributedString alloc] initWithString:@"请在前发货" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:RZ_COLOR(178, 178, 178)}];
            NSAttributedString *as2 = [[NSAttributedString alloc] initWithString:timer attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:RZ_COLOR(255, 26, 26)}];
            [mas appendAttributedString:as1];
            [mas insertAttributedString:as2 atIndex:2];
            _desL.attributedText = mas;
            _handleBtn.hidden = YES;
            _desL.hidden = NO;
            _statusBtn.userInteractionEnabled = YES;
            [_statusBtn setTitle:@"去发货" forState:UIControlStateNormal];
            break;
        }
        case GHSOrderType_DSH:{
            [self setupHandleBtnWithTitle:@"查看物流"];
            [_statusBtn setTitle:@"已发货" forState:UIControlStateNormal];
            break;
        }
        case GHSOrderType_DPJ:{
            [self setupHandleBtnWithTitle:@"查看评论"];
            [_statusBtn setTitle:@"已收货" forState:UIControlStateNormal];
            break;
        }
        case GHSOrderType_TH:{
            [self setupHandleBtnWithTitle:@"查看详情"];
            [_statusBtn setTitle:@"退货退款" forState:UIControlStateNormal];
            break;
        }
        case GHSOrderType_HH:{
            [self setupHandleBtnWithTitle:@"查看详情"];
            [_statusBtn setTitle:@"换货" forState:UIControlStateNormal];
            break;
        }
    }
}



- (void)setupHandleBtnWithTitle:(NSString *)title
{
    [_handleBtn setTitle:title forState:UIControlStateNormal];
    _handleBtn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
    [_handleBtn setTitleColor:RZ_COLOR(89, 11, 195) forState:UIControlStateNormal];
//    [_handleBtn setBackgroundImage:[UIImage imageNamed:@"按钮2"] forState:UIControlStateNormal];
    _handleBtn.layer.cornerRadius = _handleBtn.height*0.5;
    _handleBtn.layer.borderWidth = 2;
    _handleBtn.layer.borderColor = RZ_COLOR(48, 35, 174).CGColor;
}




- (void)addTags
{
    NSArray<NSString *> *items = @[@"星迹版",@"双卡双待"];
    [items enumerateObjectsUsingBlock:^(NSString * _Nonnull str, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *label = [UILabel new];
        [self.tagsView addSubview:label];
        label.text = str;
        label.textColor = RZ_COLOR(74, 74, 74);
        label.font = [UIFont fontWithName:@"PingFangSC-Light" size:12];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = RZ_COLOR(241, 241, 241);
        label.layer.cornerRadius = 2;
        label.clipsToBounds = YES;
        [self.tagItems addObject:label];
    }];
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self setTagsViewFrame];
    [self layoutIfNeeded];
    //减少离屏渲染时间 提高性能
    self.content_bgV.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.content_bgV.bounds].CGPath;
    self.image_bgV.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.image_bgV.bounds].CGPath;

}


- (void)setTagsViewFrame
{
    [self.tagsView layoutIfNeeded];
    CGSize size = self.tagsView.size;
    __block CGFloat x = 0;
    __block CGFloat y = 0;
    __block CGFloat width = 0;
    CGFloat height = 23;
    CGFloat margin = 5;
    [self.tagItems enumerateObjectsUsingBlock:^(UILabel * _Nonnull label, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [label sizeToFit];
        width = label.width+10;
        if(idx == 0){
            x = 0;
            y = 0;
            
        }else{
            
            x = CGRectGetMaxX(self.tagItems[idx-1].frame)+margin;
            if(x + width > size.width){//换行
                x = 0;
                y = CGRectGetMaxY(self.tagItems[idx-1].frame) + margin;
            }else{
                y = CGRectGetMinY(self.tagItems[idx-1].frame);
            }
        }
        
        label.frame = CGRectMake(x, y,width,height);
    }];
}


- (IBAction)statusBtnOnClick {
    !self.handleBtnBlock?:self.handleBtnBlock(_type);
}

- (IBAction)handleOnClick {
    !self.handleBtnBlock?:self.handleBtnBlock(_type);
}


#pragma mark--setter&&getter
- (NSMutableArray<UILabel *> *)tagItems
{
    if(!_tagItems){
        _tagItems = [NSMutableArray array];
    }
    return _tagItems;
}
@end
